/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddEmployee.java
 *
 * Created on 11 2, 11, 7:22:06 PM
 */

/**
 *
 * @author kylle
 */

//import java.beans.Statement;
import java.sql.*;
import javax.swing.JOptionPane;

public class AddEmployee extends javax.swing.JFrame {

    private int empIDNumber;
    /** Creates new form AddEmployee */
    public AddEmployee() {
        initComponents();
         this.setBounds(500, 100, 400, 450);
         this.setTitle("Add Employee Information");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        fnameAdd_text = new javax.swing.JTextField();
        positionAdd_text = new javax.swing.JTextField();
        AddBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        deptAdd_Box = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lnameAdd_text = new javax.swing.JTextField();
        passwordAdd_text = new javax.swing.JPasswordField();
        cpasswordAdd_text = new javax.swing.JPasswordField();
        hiringDateAdd_text = new com.toedter.calendar.JDateChooser();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jLabel1.setText("Add Employee");

        jLabel3.setText("First Name");

        jLabel5.setText("Department");

        jLabel6.setText("Position");

        jLabel7.setText("Hiring Date");

        AddBtn.setText("Add");
        AddBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AddBtnMouseClicked(evt);
            }
        });
        AddBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Cancel");
        cancelBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelBtnMouseClicked(evt);
            }
        });

        jLabel4.setText("Password");

        deptAdd_Box.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sales", "Office", "Distribution", " " }));
        deptAdd_Box.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deptAdd_BoxActionPerformed(evt);
            }
        });

        jLabel8.setText("Confirm Password");

        jLabel9.setText("Last Name");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(293, 293, 293)
                        .addComponent(jLabel10))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addComponent(jLabel1)))
                .addGap(81, 81, 81))
            .addGroup(layout.createSequentialGroup()
                .addGap(72, 72, 72)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel9)))
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(hiringDateAdd_text, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(fnameAdd_text, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(cpasswordAdd_text, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(passwordAdd_text, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(positionAdd_text, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(deptAdd_Box, 0, 100, Short.MAX_VALUE)
                            .addComponent(lnameAdd_text, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                            .addComponent(cancelBtn)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(AddBtn)))
                .addGap(72, 72, 72))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fnameAdd_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lnameAdd_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(deptAdd_Box, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(positionAdd_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(hiringDateAdd_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(cpasswordAdd_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(passwordAdd_text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddBtn)
                    .addComponent(cancelBtn))
                .addGap(91, 91, 91))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelBtnMouseClicked
        new AdminScreen2().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_cancelBtnMouseClicked

    private void AddBtnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddBtnMouseClicked
        Statement stat1;
        Connection con1 = Connector.get();

        String empFname = (fnameAdd_text.getText());
        String empLname = (lnameAdd_text.getText());
        String empDept = (deptAdd_Box.getSelectedItem().toString());
        String empPosition = (positionAdd_text.getText());
        String empHiringDate = (hiringDateAdd_text.getDate().toString());
        String empPassword = (passwordAdd_text.getText());
        String empConfirmPassword = (cpasswordAdd_text.getText());

        int month1 = hiringDateAdd_text.getDate().getMonth() + 1;
        int year1 = hiringDateAdd_text.getDate().getYear() + 1900;
        int day1 = hiringDateAdd_text.getDate().getDate();


        if(fnameAdd_text.getText().equals("") || lnameAdd_text.getText().equals("")
                || positionAdd_text.getText().equals("") || hiringDateAdd_text.getDate().getDate() == 0
                || passwordAdd_text.getText().equals("")
                || cpasswordAdd_text.getText().equals("") )
            JOptionPane.showMessageDialog(null,"Missing Information","Error", JOptionPane.ERROR_MESSAGE);

        else if(passwordAdd_text.getText().length()<5)
            JOptionPane.showMessageDialog(null,"Password is too short","Error", JOptionPane.ERROR_MESSAGE);
        else if(!passwordAdd_text.getText().equals(cpasswordAdd_text.getText()))
            JOptionPane.showMessageDialog(null,"Password does not match","Error", JOptionPane.ERROR_MESSAGE);

        else{
            try{


                stat1 = con1.createStatement();
                stat1.executeUpdate("INSERT INTO `medmartpayroll_db`.`employeeinfo` (`EmployeeID`, `FirstName`, `LastName`, `Position`, `Department`, `HiringDate`, `Password`) VALUES ((select E.EmployeeID+1 from employeeinfo E order by E.EmployeeID desc limit 1), '"+ empFname +"', '"+ empLname +"', '"+ empDept +"', '"+ empPosition +"', '"+ year1 + "-"  + month1 + "-" + day1 +"', '"+ empPassword +"')");
                stat1.executeUpdate("INSERT INTO `medmartpayroll_db`.`salaryinfo` (`EmployeeID`, `FirstName`, `LastName`) VALUES ((select E.EmployeeID from employeeinfo E order by E.EmployeeID desc limit 1), '"+ empFname +"', '"+ empLname +"')");
                JOptionPane.showMessageDialog(null,"Successfully added to database!","Added to Database", JOptionPane.INFORMATION_MESSAGE);
                }

                catch(SQLException e){
                e.printStackTrace();
               }
        }
       
       

       
           
        

    }//GEN-LAST:event_AddBtnMouseClicked

    private void deptAdd_BoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deptAdd_BoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_deptAdd_BoxActionPerformed

    private void AddBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddBtnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_AddBtnActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddEmployee().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddBtn;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JPasswordField cpasswordAdd_text;
    private javax.swing.JComboBox deptAdd_Box;
    private javax.swing.JTextField fnameAdd_text;
    private com.toedter.calendar.JDateChooser hiringDateAdd_text;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField lnameAdd_text;
    private javax.swing.JPasswordField passwordAdd_text;
    private javax.swing.JTextField positionAdd_text;
    // End of variables declaration//GEN-END:variables

}
